package com.example.kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

/**
 * @author lbq
 * @version 1.0
 * @date 2022/3/26 22:10
 */
public class MyKafkaProducer {
    public static void main(String[] args) throws InterruptedException {
        final Properties properties = new Properties();
        properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "aliyun:9092");
        properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.setProperty(ProducerConfig.ACKS_CONFIG, "1");

        final KafkaProducer<String, String> producer = new KafkaProducer<>(properties);

        int i = 1;
        String[] keys = {"01", "02", "03"};
        while (true) {
            final String key = keys[i % 3];
            String value = key + "," + System.currentTimeMillis();
            producer.send(new ProducerRecord<>("flink-demo", key, value));
            if (i > 30) {
                break;
            }
            i++;
            TimeUnit.SECONDS.sleep(1);
        }

    }
}
